package com.wlz.pipeline.server.in;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;

/**
 * 源码学院-ant
 * 只为培养BAT程序员而生
 * http://bat.ke.qq.com
 * 往期视频加群:516212256 暗号:6
 */
@ChannelHandler.Sharable/*该handler能在多个线程间共享，那么实现必须是线程安全的*/
public class NettyInBServerHandler extends ChannelInboundHandlerAdapter {

    public NettyInBServerHandler(){

    }

    @Override
    public void channelRead(ChannelHandlerContext ctx,Object msg)
    throws  Exception{
        System.out.println("进入到入站处理器 in B");
        ByteBuf outBuffer = Unpooled.copiedBuffer("antt", CharsetUtil.UTF_8);
        ctx.writeAndFlush(outBuffer);
        ctx.fireChannelRead(msg);
    }


    @Override
    public void channelReadComplete(ChannelHandlerContext ctx){
//        System.out.println("inHandler channelReadComplete");
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause){
        cause.printStackTrace();
        ctx.close();
    }

    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        super.channelRegistered(ctx);
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
//        System.out.println("NettyServerHandler channelActive");
        super.channelActive(ctx);
    }
}
